package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ActivityProcess;
import com.mofang.powerdekorhelper.model.CreateOrder;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ProcessView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/26 0026.
 * 说明：
 */

public class ProcessPersenter extends BasePresent<ProcessView> {

    RetrofitSerives retrofitSerives;
    Call<ActivityProcess> processCall;
    Call<ResultMessage> messageCall;
    Call<CreateOrder> createOrderCall;
    Call<OrderDetial> orderDetialCall;

    public void getActivityProcess(Integer customerId, Integer activityId, Integer sellerId, Integer shop_id) {

        String jsonParams;

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customer_id", customerId);
            jsonObject.put("activity_id", activityId);
            jsonObject.put("sellerId", sellerId);
            jsonObject.put("shop_id", shop_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        L.i("ProcessPersenter", "getActivityProcess-------------->" + jsonParams);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        processCall = retrofitSerives.getActivityProcess(body);

        processCall.enqueue(new Callback<ActivityProcess>() {
            @Override
            public void onResponse(Call<ActivityProcess> call, Response<ActivityProcess> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setActivityProcess(response.body());
                    } else {
                        view.showNullLayout();
                        view.toast(Constants.NONE_CUSTOMER_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ActivityProcess> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getSignUp(Integer customerId, Integer activityId, Integer signState, String signGift) {

        String jsonParams = null;

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customer_id", customerId);
            jsonObject.put("activity_id", activityId);
            jsonObject.put("sign_state", signState);
            jsonObject.put("sign_gift", signGift);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        messageCall = retrofitSerives.getSignUp(body);

        messageCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {

                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {

                        view.setSign(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    public void getSureBuy(Integer customerId, Integer activityId, Integer card) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customer_id", customerId);
            jsonObject.put("activity_id", activityId);
            jsonObject.put("card", card);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        messageCall = retrofitSerives.getSureBuy(body);

        messageCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setBuySure(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }

                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {

                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    // 核销与使用卡券
    public void getOffCoupons(Integer id, Integer giftId, Integer activityId, String gift, Integer status) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);

            if (giftId != null) {
                jsonObject.put("giftId", giftId);
            }

            jsonObject.put("activityId", activityId);
            jsonObject.put("status", status);

            if (gift != null) {
                jsonObject.put("gift", gift);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        L.i("ProcessPersenter", "getOffCoupons-------------->" + jsonParams);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        messageCall = retrofitSerives.getOffCoupons(body);

        messageCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setOffOrder(response.body());
                    } else {
                        view.onError(Constants.OFF_COUPON_INFO);
                    }

                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    // 创建订单
    public void getCreateOrder(String customerPhone, String orderCode) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customerPhone", customerPhone);
            jsonObject.put("orderCode", orderCode);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        L.i("ProcessPersenter", "jsonParams----------->" + jsonParams);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        createOrderCall = retrofitSerives.getCreateOrder(body);

        createOrderCall.enqueue(new Callback<CreateOrder>() {
            @Override
            public void onResponse(Call<CreateOrder> call, Response<CreateOrder> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setCreateOrder(response.body());
                    } else if (response.body().getCode().equals("2")) {
                        view.setCreateOrder(response.body());
                        //view.onError(Constants.OFF_ORDER_ALREADY_EXIST);
                        //view.showNullLayout();
                    } else if (response.body().getCode().equals("1")) {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CreateOrder> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 订单详情
     *
     * @param jsonParams
     */
    public void getCheckOrder(String jsonParams) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        orderDetialCall = retrofitSerives.getCheckOrder(body);
        orderDetialCall.enqueue(new Callback<OrderDetial>() {
            @Override
            public void onResponse(Call<OrderDetial> call, Response<OrderDetial> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {

                        // if (response.body().getResult() != null) {
                        view.setCheckOrder(response.body());

//                        } else {
//                            view.showNullLayout();
//                        }
                    } else if (response.body().getCode().equals("2")) {
                        view.onError(Constants.ORDER_CODE_EXIST);
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }

                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<OrderDetial> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
